package weixinkeji.dev.dc.buy.order.h.fwy;

import sys.wx.ann.hm.H;
import sys.wx.ann.hm.M;
import sys.wx.ann.power.head.SQ;
import sys.wx.c.ATR;
import sys.wx.c.components.cv.CV;
import sys.wx.m.db.dao.mysql.Dao;
import weixinkeji.dev.dc.DCPower;
import weixinkeji.dev.dc.base.food.bean.Food;
import weixinkeji.dev.dc.base.scheme.combo.bean.Combo;
import weixinkeji.dev.dc.base.scheme.promotions.bean.PromotionDay;
import weixinkeji.dev.dc.buy.order.bean.FWYOrder;
import weixinkeji.dev.dc.buy.order.bean.FWYOrderB;
import weixinkeji.dev.dc.buy.order.h.fwy.vo.FWYOrderVo;
import weixinkeji.dev.dc.buy.order.h.fwy.vo.check.CheckFWYCarVo;
import weixinkeji.dev.dc.buy.order.so.FWYOrderSO;
import weixinkeji.dev.dc.buy.order.xy.FWYOrderXY;

@H("weixinkeji/dev/dc/buy/order/a")
//身份类型授权。无需人工付权。
//代表 是服务员才能进行访问。
@SQ(DCPower.ID_FWY)
public class FWYKDBHA {

    @M("/add")//通过 weixinkeji/dev/dc/buy/order/a/add.imy 访问
    public static String add(ATR a) {
        FWYOrderVo vo = CV.getObjectByFields(FWYOrderVo.class, a.request, CheckFWYCarVo.doCar_FWY());
        if (null == vo) {
            return null;
        }
        //取出要操作的订单表头ID
        Object order_id = a.session.getAttribute(FWYOrderXY.H_MYSELECT_ORDER);
        if (null == order_id) {
            a.out_OK_NO(false, "请先选择要操作的订单。如果还没开单，请先开单！");
            return null;
        }
//        FWYOrderB lb = new FWYOrderB();

        //检查订单是否已结账订单
        FWYOrder fwyobj = Dao.selectOneByID(FWYOrder.class, order_id.toString());
        if (fwyobj.getOrder_sort().equals(FWYOrderXY.H_PAY_MONEY_ORDER)) {
            a.out_OK_NO(false, "订单已经结账，无法追加菜");
            return null;
        }
        //检查要加入订单的食物是否已经存在。如果有，进行数量+，同时修改订单头的金额。
        FWYOrderB lb = FWYOrderSO.getFWYOrderB(vo.id, order_id.toString());
        if (null != lb) {
            lb.setBuy_number(lb.getBuy_number() + vo.number);//数量增加+再次购卖的数量
            lb.setBuy_sum(1000 * lb.getBuy_price() * lb.getBuy_number() / 1000);//重新计算总的价格。由数据库自己统计
            switch (vo.sort) {
                case FWYOrderXY.B_SOURCE_SORT_COMBO: {
                    Combo cobj = Dao.selectOneByID(Combo.class, vo.id);
                    //如果是非启用，无法添加套餐
                    if (!cobj.getCombo_style().equals("1")) {
                        a.out_OK_NO(false, "对不起,套餐已经下架，无法加入清单");
                        return null;
                    }
                }
                //菜谱
                case FWYOrderXY.B_SOURCE_SORT_FOOD: {
//                    FWYOrderSO.updateOrderB(db_lb);
                    a.out_OK_NO(FWYOrderSO.updateOrderB(lb), "已加入清单");
                    return null;
                }//活动谱
                case FWYOrderXY.B_SOURCE_SORT_PROMOTIONDAY: {
//                    addPromotionDay(a, vo, lb);
                    PromotionDay pobj = Dao.selectOneByID(PromotionDay.class, vo.id);
                    if (null == pobj) {
                        a.out_OK_NO(false, "非常对不起，活动已失效");
                        return null;
                    } else if (pobj.getPromotion_now_number() < 1 || pobj.getPromotion_now_number() < vo.number) {
                        a.out_OK_NO(false, "非常对不起，活动数据不足于购卖");
                        return null;
                    }
                    a.out_OK_NO(FWYOrderSO.updateOrderB_PromotionDay(lb, vo.number));
                    return null;
                }
            }
        }
        lb = new FWYOrderB();
        lb.setOrder_id(order_id.toString());
        lb.setOrderb_style(FWYOrderXY.B_STYLE_1);
        lb.setBuy_number(vo.number);
        lb.setOrderb_lock_number(0.0);
        switch (vo.sort) {
            //菜谱
            case FWYOrderXY.B_SOURCE_SORT_FOOD: {
                Food fobj = Dao.selectOneByID(Food.class, vo.id);
                lb.setBuy_name(fobj.getFood_name());
                lb.setBuy_price(fobj.getFood_price());
                lb.setBuy_sum(1000 * fobj.getFood_price() * vo.number / 1000);
                lb.setSource_id(fobj.getFood_id());
                lb.setSource_sort(FWYOrderXY.B_SOURCE_SORT_FOOD + "");
                a.out_OK_NO(FWYOrderSO.addOrderB(lb), "已加入清单");
                return null;
            }

            //套餐谱
            case FWYOrderXY.B_SOURCE_SORT_COMBO: {
                Combo cobj = Dao.selectOneByID(Combo.class, vo.id);
                //如果是非启用，无法添加套餐
                if (!cobj.getCombo_style().equals("1")) {
                    a.out_OK_NO(false, "对不起,套餐已经下架，无法加入清单");
                    return null;
                }
                lb.setBuy_name(cobj.getCombo_name());
                lb.setBuy_price(cobj.getCombo_price());
                lb.setBuy_sum(1000 * cobj.getCombo_price() * vo.number / 1000);
                lb.setSource_id(cobj.getCombo_id());
                lb.setSource_sort(FWYOrderXY.B_SOURCE_SORT_COMBO + "");
                a.out_OK_NO(FWYOrderSO.addOrderB(lb));
                return null;
            }
            //活动谱
            case FWYOrderXY.B_SOURCE_SORT_PROMOTIONDAY: {
                addPromotionDay(a, vo, lb);
                return null;
            }
        }
        return null;
    }

    synchronized public static void addPromotionDay(ATR a, FWYOrderVo vo, FWYOrderB lb) {
        PromotionDay pobj = Dao.selectOneByID(PromotionDay.class, vo.id);
        if (null == pobj) {
            a.out_OK_NO(false, "非常对不起，活动已失效");
            return;
        } else if (pobj.getPromotion_now_number() < 1 || pobj.getPromotion_now_number() < vo.number) {
            a.out_OK_NO(false, "非常对不起，活动数据不足于购卖");
            return;
        }
        lb.setBuy_name(pobj.getFood_name());
        lb.setBuy_price(pobj.getPromotion_price());
        lb.setBuy_sum(1000 * pobj.getPromotion_price() * vo.number / 1000);
        lb.setSource_id(pobj.getPromotion_id());
        lb.setSource_sort(FWYOrderXY.B_SOURCE_SORT_PROMOTIONDAY + "");
        a.out_OK_NO(FWYOrderSO.addOrderB_PromotionDay(lb));
    }

}
